package com.wonyesoft.wuliu.acticity.user;

import java.util.HashMap;

import tools.InvitationCode;

import com.wonyesoft.wuliu.R;
import com.wonyesoft.wuliu.acticity.base.BaseActivity;
import com.wonyesoft.wuliu.api.GagApi;

import httptools.HttpJsonTool;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class RestPassWordActivity extends BaseActivity {
	private EditText user_phone;
	private EditText verification_code;
	private Button getCode;
	private EditText newPSWD;
	private EditText newPSWD_second;
	private Button resetPSWD;

	private Button closeActivity; 
	int mReSendTime=60;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_reset_pass_word);
		iniID();
		BindingAction();
	}
	
	private void iniID() {
		// TODO Auto-generated method stub
		TextView textView=(TextView) findViewById(R.id.head_title);
		textView.setVisibility(View.VISIBLE);
		textView.setText("重置密码");
		user_phone=(EditText)findViewById(R.id.user_phone);
		verification_code=(EditText)findViewById(R.id.verification_code);
		getCode=(Button)findViewById(R.id.getCode);
		newPSWD=(EditText) findViewById(R.id.newPSWD);
		newPSWD_second=(EditText) findViewById(R.id.newPSWD_second);
		resetPSWD=(Button)findViewById(R.id.resetPSWD);
		closeActivity=(Button) findViewById(R.id.closeActivity);
		ImageView imageView=(ImageView) findViewById(R.id.head_left);
		imageView.setVisibility(View.VISIBLE);
		imageView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				RestPassWordActivity.this.finish();
			}
		});
	}
	
	private void BindingAction(){
		getCode.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				getVerificationCode();
			}
		});
		
		
		resetPSWD.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				upNewPASSWORD();
			}
		});
		
		
		closeActivity.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				RestPassWordActivity.this.finish();
			}
		});
	}

	protected void upNewPASSWORD() {
		// TODO Auto-generated method stub
		HashMap< String, Object> dataMap=getUIData();
		if (dataMap==null) {
			return;
		}
		HttpJsonTool httpJsonTool=new HttpJsonTool(this);
		httpJsonTool.SetOnLister(new Handler(){
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				if (msg.what==1) {
					Toast.makeText(RestPassWordActivity.this, "重置密码成功", Toast.LENGTH_LONG).show();
					RestPassWordActivity.this.finish();
				}else {
					Toast.makeText(RestPassWordActivity.this, (String)msg.obj, Toast.LENGTH_LONG).show();
				}
			}
		});
		httpJsonTool.StartWork(dataMap, GagApi.PASS_WORD_RESET, null, null);
		
		
	}

	private HashMap<String, Object> getUIData() {
		// TODO Auto-generated method stub
		HashMap<String, Object> dataMap=new HashMap<String, Object>();
		String code=verification_code.getText().toString().trim();
		if (code.length()<=0) {
			Toast.makeText(this, "请输入验证码", Toast.LENGTH_LONG).show();
			return null;
		}
		String userPhone=user_phone.getText().toString().trim();
		if (userPhone.length()!=11) {
			Toast.makeText(this, "请输入正确的手机号码", Toast.LENGTH_LONG).show();
			return null;
		}
		String userPassword=newPSWD.getText().toString().trim();
		if (userPassword.length()<=0) {
			Toast.makeText(this, "请输入密码", Toast.LENGTH_LONG).show();
			return null;
		}
		String userPassword_2=newPSWD_second.getText().toString().trim();
		if (userPassword_2.length()<=0) {
			Toast.makeText(this, "请再次输入密码", Toast.LENGTH_LONG).show();
			return null;
		}
		
		if (!(userPassword.equals(userPassword_2))) {
			Toast.makeText(this, "2次输入的密码不一致", Toast.LENGTH_LONG).show();
			return null;
		}
		dataMap.put("code", code);
		dataMap.put("userPhone", userPhone);
		dataMap.put("userPassword", userPassword);
		
		return dataMap;
	}

	private void getVerificationCode() {
		// TODO Auto-generated method stub
		//coolingTime();
		HttpJsonTool httpJsonTool=new HttpJsonTool(this,true);
		HashMap< String, String> data=new HashMap<String, String>();
		String phone=user_phone.getText().toString().trim();
		if (phone.length()<=0) {
			Toast.makeText(this, "请输入验证码",Toast.LENGTH_LONG).show();
			return;
		}
		data.put("userPhone", InvitationCode.enCode(phone));
		httpJsonTool.SetOnLister(new Handler(){
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				if (msg.what==1) {
					Toast.makeText(RestPassWordActivity.this, "已经向您的手机发送验短信证码", Toast.LENGTH_LONG).show();
					coolingTime();
				}else {
					Toast.makeText(RestPassWordActivity.this, "请检查手机号码填写是否正确或者该号码已经被使用", Toast.LENGTH_LONG).show();
				}
			}
		});
		httpJsonTool.StartWork(data, GagApi.PASS_WORD_RESET_GET_CODE, null, null);		
	}
	
	private void coolingTime(){
		
		new CountDownTimer(60 * 1000, 1000) {
			@Override
			public void onFinish() {
				// done
				mReSendTime = 60;
				getCode.setEnabled(true);
				getCode.setText("发送验证码");
			}

			@Override
			public void onTick(long arg0) {
				// 每1000毫秒回调的方法
				getCode.setEnabled(false);
				getCode.setText("" + mReSendTime--);
			}

		}.start();
	}
	
	
}
